package com.example.taobao;

import android.app.Service;
import android.util.Log;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

    private static final int PORT = 12345;
    private List<Socket> socketList = new ArrayList<>();
    private ServerSocket serverSocket = null;
    private ExecutorService executorService = null;

    public static void main(String[] args)  {

        new Server();
    }


    public Server(){
        try{
            serverSocket = new ServerSocket(PORT);
            //创建线程池
            executorService = Executors.newCachedThreadPool();
            System.out.println("服务端运行中...\n");
            Socket client = null;
            while (true){
                //返回一个与连接客户端Socket对应的Socket
                client = serverSocket.accept();
                socketList.add(client);
                executorService.execute(new Service(client));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class Service implements Runnable{

        private Socket socket;
        private BufferedReader in = null;
        private String msg="";

        public Service(Socket socket){
            this.socket = socket;
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                msg = "用户: "+this.socket.getInetAddress()+" 加入了聊天室; "
                        +"当前在线人数:"+socketList.size();
                this.sendMsg();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            try{
                while(true){
                    if((msg = in.readLine())!=null){
                        if(msg.equals("bye")){
                            System.out.println("------------");
                            socketList.remove(socket);
                            in.close();
                            msg = "用户: "+this.socket.getInetAddress()+" 退出了聊天室\n"
                                    +"当前在线人数:"+socketList.size();
                            socket.close();
                            this.sendMsg();
                            break;
                        }
                        else {
                            msg ="用户: "+ socket.getInetAddress()+" : "+msg;
                            this.sendMsg();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //给每个连上服务端的客户端发送消息
        public void sendMsg(){
            System.out.println("msg:"+msg);
            int num = socketList.size();
            for (int i = 0; i <num ; i++) {
                Socket mSocket = socketList.get(i);
                PrintWriter out = null;
                try {
                    out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(mSocket.getOutputStream(),"UTF-8")),true);
                    out.println(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

